package com.hui.platform.falseworkapi.common.core.exception.util;


import com.hui.platform.falseworkapi.common.core.I18nMessage;
import com.hui.platform.falseworkapi.common.core.exception.BusinessException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 业务异常处理类工具类 （不建议采用）
 *
 * @author Peng
 * @date 2019/10/23
 * @deprecated 2019/11/21
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Deprecated
public class AssertUtil {

    /**
     * 自定义异常
     *
     * @param iMessage 消息接口
     */
    private static void newException(I18nMessage iMessage) {
        throw new BusinessException(iMessage);
    }

    //--------------------------断言方法区-----------------------

    public static void state(boolean expression, I18nMessage iMessage) {
        if (!expression) {
            newException(iMessage);
        }
    }

    public static void isTrue(boolean expression, I18nMessage iMessage) {
        if (!expression) {
            newException(iMessage);
        }
    }

    public static void isNull(@Nullable Object object, I18nMessage iMessage) {
        if (object != null) {
            newException(iMessage);
        }
    }

    public static void notNull(@Nullable Object object, I18nMessage iMessage) {
        if (object == null) {
            newException(iMessage);
        }
    }

    public static void hasLength(@Nullable String text, I18nMessage iMessage) {
        if (!StringUtils.hasLength(text)) {
            newException(iMessage);
        }
    }

    public static void hasText(@Nullable String text, I18nMessage iMessage) {
        if (!StringUtils.hasText(text)) {
            newException(iMessage);
        }
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, I18nMessage iMessage) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            newException(iMessage);
        }
    }

    public static void notEmpty(@Nullable Object[] array, I18nMessage iMessage) {
        if (ObjectUtils.isEmpty(array)) {
            newException(iMessage);
        }
    }

    public static void noNullElements(@Nullable Object[] array, I18nMessage iMessage) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    newException(iMessage);
                }
            }
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, I18nMessage iMessage) {
        if (CollectionUtils.isEmpty(collection)) {
            newException(iMessage);
        }
    }

    public static void noNullElements(@Nullable Collection<?> collection, I18nMessage iMessage) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    newException(iMessage);
                }
            }
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, I18nMessage iMessage) {
        if (CollectionUtils.isEmpty(map)) {
            newException(iMessage);
        }
    }
}
